<template>
  <div class="order">
    <div class="df">
      <el-form inline :model="formData" size="mini" class="demo-form">
        <el-form-item label="订单号">
          <el-input v-model="formData.orderNo" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item label="收货人">
          <el-input
            v-model="formData.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formData.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="formData.orderState">
            <el-option value="已受理" label="已受理"></el-option>
            <el-option value="派送中" label="派送中"></el-option>
            <el-option value="已完成" label="已完成"></el-option>
          </el-select>
        </el-form-item>
        <div class="block">
          <span class="demonstration">选择时间</span>
          <el-date-picker
            v-model="formData.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="inputday"
          ></el-date-picker>
          <!-- 查询按钮 -->
          <el-form-item>
            <el-button @click="search" type="primary" class="button">
              查询
            </el-button>
          </el-form-item>
        </div>
        <!-- 表单 -->
        <el-table min-width :data="tableData" border :style="{width: w + 'px'}">
          <!-- 订单号 -->
          <el-table-column prop="orderNo" label="订单号"></el-table-column>
          <!-- 日期 -->
          <el-table-column label="下单时间">
            <template slot-scope="scope">
              {{ scope.row.orderTime | formatHms }}
            </template>
          </el-table-column>
          <!-- 手机号x -->
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <!-- 姓名 -->
          <el-table-column prop="consignee" label="姓名"></el-table-column>
          <!-- 地址 -->
          <el-table-column
            prop="deliverAddress"
            label="送货地址"
          ></el-table-column>
          <!-- 到达日期 -->
          <el-table-column label="到达时间">
            <template slot-scope="scope">
              {{ scope.row.deliveryTime | formatHms }}
            </template>
          </el-table-column>
          <!-- 用户备注 -->
          <el-table-column prop="remarks" label="用户备注"></el-table-column>
          <!-- 订单金额 -->
          <el-table-column
            prop="orderAmount"
            label="订单金额"
          ></el-table-column>
          <!-- 订单状态 -->
          <el-table-column prop="orderState" label="订单状态"></el-table-column>
          <!-- 操作 -->
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="
                  $router.push({
                    path: '/order/order-detail',
                    query: {id: scope.row.id},
                  })
                "
                size="small"
              >
                查看
              </el-button>
              <el-button
                type="text"
                @click="$router.push(`/order/order-edit/${scope.row.id}`)"
                size="small"
              >
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="margin-top: 20px"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total,  prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-form>
      <!-- 表格 -->
    </div>
  </div>
</template>

<script>
//引入订单列表接口
import {getOrderListReq} from '@/api/order'
import resizeTableW from '@/mixins/resizeTable'
export default {
  mixins: [resizeTableW],
  data() {
    return {
      // 查询表单数据
      formData: {
        orderNo: '', //订单号
        consignee: '', //收货人
        phone: '', //手机号
        orderState: '', //订单状态
        date: [], //日期
      },
      // 总页数
      total: 0,
      currentPage: 1,
      // 每页显示
      pageSize: 3,
      // 表单数据
      tableData: [],
      //查询表单数据
      searchForm: {},
    }
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    // 发送请求
    async getData() {
      let pageSize = this.pageSize
      let currentPage = this.currentPage
      //处理日期格式 处理默认值null问题
      let date = JSON.stringify(this.searchForm.date || '')

      let res = await getOrderListReq({
        pageSize,
        currentPage,
        ...this.searchForm, //查询表单数据进来
        date,
      })
      // 解构数据
      let {total, data} = res.data
      // 将数据赋值
      this.total = total
      this.tableData = data

      //如果获取data为[]  且currentPage>1自动返回上一页
      if (data.length === 0 && this.currentPage !== 1) {
        this.currentPage--
        this.getData()
      }
    },
    //查询
    search() {
      //把当前页重置为1
      this.currentPage = 1

      //把当前formData的值 拷贝给查询表单使用
      this.searchForm = {...this.formData}

      this.getData()
    },
    // 改变当前页面
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    },
  },
  created() {
    // 初始化发送请求
    this.getData()
  },
}
</script>

<style lang="scss" scoped>
.demo-form {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 20px;
}
.el-input__inner {
  height: 32px;
  width: 120px;
  font-size: 12px;
}
.el-input__inner {
  width: 400px;
}
.demonstration {
  margin-right: 10px;
  color: #606266;
}
.button {
  margin-left: 20px;
}
.inputday {
  height: 40px;
}
</style>
